<template>
  <div>
    <div id="upload">
      <!--elementui的上传图片的upload组件-->
      <el-upload
        class="upload-demo"
        :limit="1"
        ref="upload"
        list-type="picture-card"
        action="http://localhost:3000/personal/UpdHeadPic"
        :before-upload="beforeupload"
        :auto-upload="false"
        :multiple="true"
      >
        <i class="el-icon-plus"></i>
      </el-upload>
      <el-button type="primary" @click="onSubmit">更新</el-button>
      <el-button>取消</el-button>
    </div>
  </div>
</template>
<script>
export default {
  name: "test",
  data() {
    return {
      company_id: "10001",
      param: "" // 表单最后提交的参数对象
    };
  },
  methods: {
    toinfo() {
      this.$router.push("PersonInfo");
    },
    onSubmit() {
      let _this = this;
      this.$refs.upload.submit();
      //将非表单元素的数据也添加到参数对象中；
      this.param.append("company_id", _this.company_id);
      //设置提交请求头，适用于上传文件
      let config = {
        headers: {
          "Content-Type": "multipart/form-data"
        }
      };
      // //调用接口，执行上传所有数据的操作
      this.$axios
        .post("http://localhost:3000/personal/UpdHeadPic", this.param, config)
        .then(function(result) {});
      // setTimeout(function() {
      //   alert("ok");
      //   this.toinfo()
      // }, 1000);
      this.$router.push("PersonInfo");
    },

    //当上传文件组件submit之前触发执行
    beforeupload(file) {
      this.param = new FormData();
      this.param.append("headPic", file);
      return false;
    }
  }
};
</script>

